package com.origin.onlinesys.web.controllers.modules.home;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.origin.onlinesys.common.controllers.BaseController;
import com.origin.onlinesys.domain.modules.interviewsys.SmsHistory;
import com.origin.onlinesys.domain.modules.interviewsys.SmsTemp;
import com.origin.onlinesys.service.modules.finance.impl.FinanceServiceImpl;
import com.origin.onlinesys.service.modules.user.impl.RolesServiceImpl;
import com.origin.onlinesys.service.modules.user.impl.SmsHistoryServiceImpl;
import com.origin.onlinesys.service.modules.user.impl.SmsTempServiceImpl;
import com.origin.onlinesys.web.utils.SMSUtils;
import com.origin.onlinesys.web.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/27 0027.
 */
@Controller
@RequestMapping(value = "/sms")
public class SMSController extends BaseController {

    @Autowired
    private SmsTempServiceImpl service;

    @Autowired
    private SmsHistoryServiceImpl historyService;


    @RequestMapping(value = "/smsIndex")
    public ModelAndView smsIndex(){
        String zoneId = WebUtils.getAccount().getQueryZoneId();
        SmsTemp data = service.selectByZoneId(zoneId);
        System.out.println(data);
        return new ModelAndView("pages/sms/index","data",data);
    }

    @RequestMapping(value = "/smsSend",method = RequestMethod.POST)
    public @ResponseBody
    JSONObject smsSend(String phoneNum,String intevDate,String contact,String smsTxt,String tempCode,String signName){
        JSONObject object = new JSONObject();
        object.put("state",200);
        object.put("msg","成功发送");
        SmsHistory history = new SmsHistory();
        history.setContent(smsTxt);
        history.setUserId(WebUtils.getAccount().getId());
        history.setPhoneNum(phoneNum);
        history.setSendDate(System.currentTimeMillis()+"");
        history.setSendState("1");
        try {
            SMSUtils.sendSms(signName,tempCode,phoneNum,intevDate,contact);
        } catch (Exception e) {
            e.printStackTrace();
            object.put("state",-1);
            object.put("msg",e.getMessage());
            history.setSendState("-1");
        }
        historyService.save(history);
        return object;
    }

}
